package com.wn.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.dao.HotelinfoDao;
import com.wn.entity.Area;
import com.wn.entity.Hotelinfo;
import com.wn.entity.Travelinfo;
import com.wn.service.AreaService;
import com.wn.service.HotelinfoService;
import com.wn.utils.PageParam;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * (Hotelinfo)表服务实现类
 *
 * @author makejava
 * @since 2020-10-26 09:30:32
 */
@Service("hotelinfoService")
@Transactional(rollbackFor = Exception.class)
public class HotelinfoServiceImpl implements HotelinfoService {
    @Resource
    private HotelinfoDao hotelinfoDao;

    @Resource
    private AreaService areaService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Hotelinfo queryById(String id) {
        Hotelinfo hotelinfo = this.hotelinfoDao.queryById(id);
        //根据地区id拿到地区具体值，赋值给酒店信息中
        int areaid = Integer.parseInt(hotelinfo.getAreaid());
        Area area = areaService.queryById(areaid);
        hotelinfo.setArea(area);
        return hotelinfo;
    }


    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public PageInfo<Hotelinfo> queryAllByLimit(PageParam<Hotelinfo> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Hotelinfo> travelList=hotelinfoDao.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Hotelinfo> travelPageInfo = new PageInfo<Hotelinfo>(travelList);

            return travelPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }


            List<Hotelinfo> newsinfoList=hotelinfoDao.searchSomeByPage(pageParam.getModel());
            PageInfo<Hotelinfo> newsinfoPageInfo = new PageInfo<Hotelinfo>(newsinfoList);

            return newsinfoPageInfo;

        }

    }

    /**
     * 新增数据
     *
     * @param hotelinfo 实例对象
     * @return 实例对象
     */
    @Override
    public Hotelinfo insert(Hotelinfo hotelinfo) {
        //生成随机酒店方式id
        String hotelid= UUID.randomUUID().toString().replaceAll("-","")+"_hotel";
        hotelinfo.setId(hotelid);
        this.hotelinfoDao.insert(hotelinfo);
        return hotelinfo;
    }

    /**
     * 修改数据
     * @param hotelinfo 实例对象
     * @return 实例对象
     */
    @Override
    public Hotelinfo update(Hotelinfo hotelinfo) {
        this.hotelinfoDao.update(hotelinfo);
        return this.queryById(hotelinfo.getId());
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return 实例对象
     */
    @Override
    public Hotelinfo deleteLogical(String id) {
        this.hotelinfoDao.deleteLogical(id);
        return this.queryById(id);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.hotelinfoDao.deleteById(id) > 0;
    }

    @Override
    public List<Hotelinfo> selHotel(Object longitude, Object latitude) {
        List<Hotelinfo> hotelinfos = hotelinfoDao.selHotel(longitude, latitude);
        return hotelinfos;
    }
}